<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useCandidateStore } from '@/stores'
import { candidateUpdatePassService } from '@/api/candidate'

const formRef = ref()
const router = useRouter()
const candidateStore = useCandidateStore()
const loading = ref(false)

/*提交修改*/
const onSubmit = async () => {
  const valid = await formRef.value.validate()
  loading.value = true
  if (valid) {
    // 难受（
    await candidateUpdatePassService({
      password: pwdForm.value.new_pwd,
      username: candidateStore.candidate.username
    }),
      (loading.value = false)
    ElMessage.success('更换密码成功')
    candidateStore.setToken('')
    candidateStore.setCandidate({})
    router.push('/')
  }
}

const pwdForm = ref({
  old_pwd: '',
  new_pwd: '',
  re_pwd: ''
})
/*校验方法*/
const checkOldSame = (rule, value, cb) => {
  if (value === pwdForm.value.old_pwd) {
    cb(new Error('原密码和新密码不能一样!'))
  } else {
    cb()
  }
}
const checkNewSame = (rule, value, cb) => {
  if (value !== pwdForm.value.new_pwd) {
    cb(new Error('新密码和确认再次输入的新密码不一样!'))
  } else {
    cb()
  }
}
/*校验规则*/
const rules = {
  // 原密码
  old_pwd: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    }
  ],
  // 新密码
  new_pwd: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    },
    { validator: checkOldSame, trigger: 'blur' }
  ],
  // 确认新密码
  re_pwd: [
    { required: true, message: '请再次确认新密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码长度必须是6-15位的非空字符串',
      trigger: 'blur'
    },
    { validator: checkNewSame, trigger: 'blur' }
  ]
}
</script>
<template>
  <layout-com title="重置密码">
    <el-row>
      <el-col :span="12">
        <el-form
          v-model="Loading"
          element-loading-background="rgba(141,164,195, 0.4)"
          :model="pwdForm"
          :rules="rules"
          ref="formRef"
          label-width="100px"
          size="large"
        >
          <el-form-item label="原密码" prop="old_pwd">
            <el-input
              v-model="pwdForm.old_pwd"
              type="password"
              show-password="ture"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="new_pwd">
            <el-input
              v-model="pwdForm.new_pwd"
              type="password"
              show-password="ture"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="re_pwd" show-password="ture">
            <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="onSubmit"
              type="primary"
              color="#354152"
              :dark="isDark"
              >修改密码</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </layout-com>
</template>
